package com.tomshushu.doc.freedoc.modular.project.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tomshushu.doc.freedoc.core.exception.ServiceException;
import com.tomshushu.doc.freedoc.modular.project.entity.ProjectEntity;
import com.tomshushu.doc.freedoc.modular.project.mapper.ProjectMapper;
import com.tomshushu.doc.freedoc.modular.project.model.param.AddProjectParam;
import com.tomshushu.doc.freedoc.modular.project.model.param.DeleteProjectParam;
import com.tomshushu.doc.freedoc.modular.project.model.param.EditProjectParam;
import com.tomshushu.doc.freedoc.modular.project.model.result.ProjectStatisticsResult;
import com.tomshushu.doc.freedoc.modular.project.service.ProjectService;
import com.tomshushu.doc.freedoc.modular.projectuser.entity.ProjectUserEntity;
import com.tomshushu.doc.freedoc.modular.projectuser.model.param.AddProjectUserParam;
import com.tomshushu.doc.freedoc.modular.projectuser.service.ProjectUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl  extends ServiceImpl<ProjectMapper, ProjectEntity> implements ProjectService {

    @Resource
    private ProjectUserService projectUserService;

    @Override
    public void addProject(AddProjectParam addProjectParam) {
        long count = this.count(new LambdaQueryWrapper<ProjectEntity>()
                .eq(ProjectEntity::getProjectName, addProjectParam.getProjectName())
                .eq(ProjectEntity::getTeamId, addProjectParam.getTeamId()));
        if (count > 0) {
            throw new ServiceException(7004, "团队已存在同名项目");
        }
        String userId = StpUtil.getLoginIdAsString();
        ProjectEntity projectEntity = new ProjectEntity();
        BeanUtils.copyProperties(addProjectParam, projectEntity);
        projectEntity.setCreateUser(userId);
        save(projectEntity);
        // 添加用户项目关系
        AddProjectUserParam param = new AddProjectUserParam();
        AddProjectUserParam.ProjectUser projectUser = new AddProjectUserParam.ProjectUser();
        List<AddProjectUserParam.ProjectUser> projectUserList = new ArrayList<>();
        projectUser.setProjectId(projectEntity.getProjectId());
        projectUser.setUserId(userId);
        projectUser.setType("OWNER");
        projectUser.setPermission("rw");
        projectUserList.add(projectUser);
        param.setProjectUserList(projectUserList);
        projectUserService.addProjectUser(param);
    }

    @Override
    public void editProject(EditProjectParam param) {

        ProjectEntity projectEntity = this.getById(param.getProjectId());
        if (ObjectUtil.isEmpty(projectEntity)) {
            throw new ServiceException(7005, "项目不存在");
        }
        // 请求参数转化为实体
        BeanUtil.copyProperties(param, projectEntity);
        this.updateById(projectEntity);
    }

    @Override
    public void deleteProject(DeleteProjectParam param) {
        // 删除项目
        this.removeById(param.getProjectId());
        // 删除项目用户关系
        projectUserService.remove(new LambdaQueryWrapper<ProjectUserEntity>()
                .eq(ProjectUserEntity::getProjectId, param.getProjectId()));

    }

    @Override
    public ProjectStatisticsResult statistics(DeleteProjectParam param) {
        return null;
    }

    @Override
    public ProjectEntity getProjectInfo(DeleteProjectParam param) {
        if (ObjectUtil.isEmpty(param.getProjectId())) {
            throw new ServiceException(7005, "项目不存在");
        }
        return this.getById(param.getProjectId());
    }
}